package com.fancyy.scm.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.databinding.ObservableField;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.fancyy.scm.R;
import com.fancyy.scm.base.BaseActivity;
import com.fancyy.scm.base.adapter.BRBaseAdapter;
import com.fancyy.scm.base.annotation.Layout;
import com.fancyy.scm.base.annotation.Translucent;
import com.fancyy.scm.base.http.BRDataListener;
import com.fancyy.scm.base.http.BRDataTransport;
import com.fancyy.scm.bean.CartonDetail;
import com.fancyy.scm.bean.ErrorEnum;
import com.fancyy.scm.databinding.ActivityTrackScanBinding;
import com.fancyy.scm.util.DialogUtil;
import com.google.gson.reflect.TypeToken;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import java.util.ArrayList;
import java.util.List;

import static com.fancyy.scm.BRConstant.GETCARTONLISTBYCARCODE;
import static com.fancyy.scm.BRConstant.SCANTRAYORCARTONTOCAT;
import static com.fancyy.scm.BRConstant.SUBMITCARTONLISTTOCAR;
import static com.fancyy.scm.BRConstant.CARRESET;

@Translucent
@Layout(R.layout.activity_track_scan)
public class TrackScanActivity extends BaseActivity<ActivityTrackScanBinding> {
    public ObservableField<String> CarCode = new ObservableField<>();
    BRBaseAdapter adapter = new BRBaseAdapter();
    //    List<CartonInfoItem> list = new ArrayList<>();
    List<Object> list = new ArrayList<>();
    private boolean isErrorPopUp=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setNeedScan(true);
        super.onCreate(savedInstanceState);
        adapter.init(this, R.layout.cartondetailitem, list);
        getDataBinding().listView.setAdapter(adapter);

//        String barCode = getIntent().getStringExtra("barCode");
        String cartonNum = getIntent().getStringExtra("CartonNum");

//        getDataBinding().scan.setText(barCode);
        getDataBinding().TotalQty.setText(cartonNum);


    }

    public void onScanningClick(View view) {
        if(!getPDA()) {
            Intent intent = new Intent(this, CaptureActivity.class);
            startActivityForResult(intent, 1);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1 && resultCode == RESULT_OK) {
            String code = data.getStringExtra(CodeUtils.RESULT_STRING);
            handleCode(code);

        }
    }

    public void onSearchCartonListClick(View view){
        BRDataTransport.createGet(GETCARTONLISTBYCARCODE + "?CarCode=" + CarCode.get() )
                .addProgressing(this, "Progressing...")
                .execute(new BRDataListener<List<CartonDetail>>() {
                    @Override
                    public void onSuccess(List<CartonDetail> data) {
                        list.clear();
                        if (data != null && data.size() > 0) {
                            list.addAll(data);
                        } else {
                            showToast("No data be found");
                        }
                        adapter.notifyDataSetChanged();
                        getDataBinding().TotalQty.setText(data.size()+"");
                    }

                }, new TypeToken<List<CartonDetail>>(){});
    }

    private void handleCode(String code) {
        getDataBinding().scan.setText(code);
        BRDataTransport.create(SCANTRAYORCARTONTOCAT)
                .addParam("CarCode", CarCode.get())
                .addParam("barcode", getDataBinding().scan.getText().toString())
                .addParam("OperateUserName", getBRApplication().getUserInfo().name)
                .addParam("OperateUserAccount", getBRApplication().getUserInfo().userAccount)
                .addProgressing(this, "Progressing...")
        .execute(new BRDataListener() {
            @Override
            public void onSuccess(Object data) {
                setResult(RESULT_OK);
                onSearchCartonListClick(null);
            }
            @Override
            public void onFail(int status, String failMessage) {
                onSearchCartonListClick(null);
                //showToast(failMessage);
                new AlertDialog.Builder(TrackScanActivity.this).setTitle("Error")
                        .setMessage(failMessage)
                        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {

                            }
                        }).show();
            }
        });
    }

    @Override
    public void doScan(String data) {
        if(!isErrorPopUp)
            handleCode(data);
    }

    String remark;

    public void onResetClick(View view) {
        BRDataTransport.createPost(CARRESET)
                .addParam("CarCode", CarCode.get())
                .addParam("OperateUserName", getBRApplication().getUserInfo().name)
                .addParam("OperateUserAccount", getBRApplication().getUserInfo().userAccount)
                .addProgressing(this, "Progressing...")
                .execute(new BRDataListener() {
                    @Override
                    public void onSuccess(Object data) {
                        getDataBinding().scan.setText("");
                        onSearchCartonListClick(null);
                        showToast("Reset Success");
                        list.clear();
                        adapter.notifyDataSetChanged();
                        getDataBinding().TotalQty.setText("");
                    }
                });
    }

    public void onSubmitClick(View view) {
        BRDataTransport.create(SUBMITCARTONLISTTOCAR)
                .addParam("CarCode", CarCode.get())
                .addParam("OperateUserName", getBRApplication().getUserInfo().name)
                .addParam("OperateUserAccount", getBRApplication().getUserInfo().userAccount)
                .addProgressing(this, "Progressing...")
                .execute(new BRDataListener() {
                    @Override
                    public void onSuccess(Object data) {
                        showToast("Submit Success");
                        list.clear();
                        adapter.notifyDataSetChanged();
                        getDataBinding().TotalQty.setText("");
                        getDataBinding().scan.setText("");
                    }
                });
    }

    List<ErrorEnum> errorData;

    public void onCancelClick(View view) {
        remark = null;
        getDataBinding().submit.setVisibility(View.GONE);
        isErrorPopUp = false;
    }

    public void onErrorCodeClick(View view) {
        DialogUtil.showList(self(), "Error Item", errorData, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int which) {
                remark = errorData.get(which).enumCode;
                TextView t = findViewById(R.id.code);
                t.setText(errorData.get(which).enumName);
                dialogInterface.dismiss();
            }
        }, true);
    }

}
